import json
from models import BaseModels
from sqlalchemy import Column, ForeignKey, DateTime, BigInteger, Integer, JSON, Boolean, Text, String, Uuid


class Detailed(BaseModels):
    __tablename__ = 'detailed'
    id = Column(BigInteger, autoincrement=True, primary_key=True, unique=True, nullable=False)
    time = Column(DateTime, index=True, nullable=False, comment="采集时间")
    sequence = Column(String(64), nullable=False, index=True, comment="产品序列SN")
    complete = Column(Boolean, default=False, comment="是全部都完成标志位, True全部完成")
    course_id = Column(Integer, ForeignKey("courses.id", ondelete='CASCADE'), index=True, comment="对应机种ID")
    aircraft_id = Column(Integer, ForeignKey("aircraft.id", ondelete='CASCADE'), index=True, comment="对应机种ID")
    tasks_id = Column(Integer, ForeignKey("tasks.id", ondelete='CASCADE'), index=True, comment="对应分任务编号ID")
    facility_id = Column(Integer, ForeignKey("facility.id", ondelete='CASCADE'), index=True, comment="经过哪台设备")
    message = Column(JSON, nullable=True, comment="采集信息, JSON 格式数据")
    finish = Column(DateTime, index=True, nullable=False, comment="结束时间")
    status = Column(Boolean, nullable=True, index=True, comment="是否合格的，读取上下限数据")
    numbers = Column(Integer, nullable=False, comment="加工序次")
    error = Column(Text, nullable=True, comment="不合格原因")
    option = Column(String(64), nullable=True, comment="操作员名字")
    mark = Column(Integer, nullable=False, comment="1oK, 2nG, 3报废")
    first = Column(Boolean, nullable=False, index=True, comment="是否首件产品")
    grade = Column(String(64), index=True, comment="打码等级")
    session = Column(Uuid, nullable=True, unique=False, comment="会话UUID4编号")

    def __repr__(self):
        return json.dumps({
            "detailed_id": self.id,
            "time": self.time.strftime("%Y-%m-%d %H:%M:%S"),
            "sequence": self.sequence
        })

